package com.fortune.controller;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.bean.OptionVo;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.faneui.common.utils.MapUtils;
import com.fortune.dal.dao.BankDAO;
import com.fortune.dal.dao.CreditCardTypeDAO;
import com.fortune.dal.model.Bank;
import com.fortune.dal.model.CreditCardType;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("creditcardtype")
public class CreditCardTypeController extends AbstractController{

    @Autowired
    private CreditCardTypeDAO creditCardTypeDAO;

    @Autowired
    private BankDAO bankDAO;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws BusinessException {
        ModelAndView mv = new ModelAndView("creditcardtype");
        mv.addObject("menuFlag", "creditcardtype");
        return mv;
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public List<Map<String,Object>> list(@ModelAttribute CreditCardType creditCardType) throws BusinessException {
        List<Map<String,Object>> list = Lists.newArrayList() ;
        try {
            List<CreditCardType> creditCardTypes = creditCardTypeDAO.queryByModel(creditCardType) ;
            if(ListUtils.isNotEmpty(creditCardTypes)) {
                for(CreditCardType cardType:creditCardTypes){
                    Map<String,Object> dataMap = MapUtils.parseMap(cardType) ;
//                    Card card = cardDAO.queryById(cardInfo.getCardId()) ;
                    Bank bank = bankDAO.queryById(cardType.getBankId()) ;
                    dataMap.put("bankName",bank.getName()) ;
                    list.add(dataMap) ;
                }
            }
        }catch (Exception ex) {
            logger.error("",ex);
            return Lists.newArrayList() ;
        }
        return list;
    }


    @RequestMapping(value = "/select/{queryType}", method = RequestMethod.GET)
    @ResponseBody
    public List<OptionVo> selectOptionVos(@PathVariable String queryType) {
        List<OptionVo> optionVos = Lists.newArrayList();
        try {
            if ("addBank".equals(queryType)) {
                List<Bank> banks = bankDAO.queryByModel(new Bank()) ;
                System.out.println(banks);
                for (Bank vo : banks) {
                    optionVos.add(new OptionVo(vo.getName(),vo.getId()+""));
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return optionVos;
    }


    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp saveProject(@RequestBody CreditCardType creditCardType) {
        BaseResp resp = new BaseResp();
        try {
            if(creditCardType.getBankId() == null) {
                System.out.println(creditCardType.getBankId()+"-----");
                throw new BusinessException("请选择银行") ;
            }
            if(StringUtils.isBlank(creditCardType.getName())) {
                throw new BusinessException("请填写卡种名称") ;
            }
            if(StringUtils.isBlank(creditCardType.getAnnualFee())) {
                throw new BusinessException("请填写年费") ;
            }
            if(creditCardType.getId() == null) {

//                Cust param = new Cust();
//                param.setName(cust.getName());
//                param.setSex(cust.getSex());
//                param.setBirth(cust.getBirth());
//                param.setPhone(cust.getPhone());
//                if(ListUtils.isNotEmpty(custDAO.queryByModel(param))) {
//                    throw new BusinessException("用户名重复") ;
//                }
                creditCardTypeDAO.insert(creditCardType);
            } else {
                creditCardTypeDAO.updateById(creditCardType) ;
            }
        } catch (Exception ex) {
            logger.error("", ex);
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }
}
